using System;
using System.Linq;
using Castle.MonoRail.Framework;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Description;
using System.Net;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Text;
using System.Web;

namespace Dashboard.Controllers
{
    public class DashboardController : SmartDispatcherController
	{
        static IEnumerable<Issue> issues;
        public static int lastVersion = 1;

		public void Index()
		{
        }

        [return: JSONReturnBinder]
        public IEnumerable<Issue> UpdateIssues(string id, string state)
        {
            Issue issue = issues.Single(i => i.Id.Equals(id));

            issue.State = state;
            issue.Version = lastVersion++;

            UpdateIssueState(issue.Id, issue.State);

            return GetIssues();
        }

        [return: JSONReturnBinder]
        public IEnumerable<Issue> GetIssues()
        {
            if (issues == null)
                LoadIssues();

            return issues.OrderBy(i => i.Version);
        }

        public void LoadIssues()
        {
            issues = new List<Issue>();
            var url = "http://ecascardo.myjetbrains.com/youtrack/rest/issue/byproject/FoF";

            var req = WebRequest.Create(url) as HttpWebRequest;

            using (var resp = req.GetResponse() as HttpWebResponse)
            {
                var reader = new XmlTextReader(resp.GetResponseStream());

                var lastField = string.Empty;
                Issue lastIssue = null;
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "issue")
                    {
                        lastIssue = new Issue();
                        lastIssue.Id = reader.GetAttribute("id");
                        lastIssue.Version = lastVersion++;
                        ((List<Issue>)issues).Add(lastIssue);
                    }

                    if (reader.NodeType == XmlNodeType.Element && reader.Name == "field")
                    {
                        lastField = reader.GetAttribute("name");
                    }

                    if (reader.NodeType == XmlNodeType.Text && lastField == "summary")
                    {
                        lastIssue.Name = reader.Value;
                    }

                    if (reader.NodeType == XmlNodeType.Text && lastField == "State")
                    {
                        lastIssue.State = reader.Value[0].ToString().ToLower()
                            + reader.Value.Substring(1, reader.Value.Length-1).Replace(" ", "");
                    }
                }
            }
        }

        public void UpdateIssueState(string id, string state)
        {
        }
    }

	public class Issue
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string State { get; set; }
        public int Version { get; set; }
    }
}
